package com.atguigu.spzx.product.controller;

import com.atguigu.spzx.model.dto.h5.ProductSkuDto;
import com.atguigu.spzx.model.dto.product.SkuSaleDto;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.h5.ProductItemVo;
import com.atguigu.spzx.product.service.ProductService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * ClassName: ProductController
 * Package: com.atguigu.spzx.product.controller
 * Description:
 *
 * @Author 刘强
 * @Create 2024/7/14 23:31
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/product")
public class ProductController {
    @Resource
    private ProductService productService;

    /**
     * /api/product
     * 跟新库存和销量 -> 库存减少，销量增加
     */
    @GetMapping("updateStockAndSaleNum")
    public void updateStockAndSaleNum(List<SkuSaleDto> skuSaleDtoList){
        productService.updateStockAndSaleNum(skuSaleDtoList);
    }
    /** /api/product/getBySkuId/{skuId}
     * 根据skuId获取sku
     * @param skuId
     * @return
     */
    @GetMapping("getBySkuId/{skuId}")
    public ProductSku getBySkuId(@PathVariable Long skuId){
        return productService.getBySkuId(skuId);
    }


    /**
     * 根据skuId查询商品详情
     * @param skuId
     * @return
     */
    @GetMapping("/item/{skuId}")
    public Result<ProductItemVo> item(@PathVariable Long skuId) {
        ProductItemVo productItemVo = productService.item(skuId);
        return Result.build(productItemVo,ResultCodeEnum.SUCCESS);
    }

    /**
     * 分页带条件查询 商品sku
     * @param page
     * @param limit
     * @param productSkuDto
     * @return
     */
    @GetMapping("{page}/{limit}")
    public Result<PageInfo<ProductSku>> findByPage(@PathVariable Integer page, @PathVariable Integer limit,
                                                   ProductSkuDto productSkuDto){
        PageInfo<ProductSku> pageInfo = productService.findByPage(page,limit,productSkuDto);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }
}
